package com.itbaizhan.openhis.controller;

import com.itbaizhan.openhis.domain.DictData;
import com.itbaizhan.openhis.dto.DictDataDto;
import com.itbaizhan.openhis.service.DictDataService;
import com.itbaizhan.openhis.utils.ShiroSecurityUtils;
import com.itbaizhan.openhis.vo.AjaxResult;
import com.itbaizhan.openhis.vo.DataGridView;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * 字典数据管理
 */
@RestController
@RequestMapping("system/dict/data")
@Slf4j
public class DictDataController {
	
	@Autowired
	private DictDataService dictDataService;
	
	/**
	 * 查询字典数据
	 * @param dictDataDto
	 * @return
	 */
	@GetMapping("listForPage")
	public AjaxResult listForPage(DictDataDto dictDataDto){
		log.info("字典数据管理查询方法,参数：{}",dictDataDto);
		DataGridView dataGridView = dictDataService.listForPage(dictDataDto);
		return AjaxResult.success("查询字典成功", dataGridView.getData(),  dataGridView.getTotal());
	}
	
	/**
	 * 根据dictId查询字典数据
	 * @param dictId
	 * @return
	 */
	@GetMapping("getOne/{dictId}")
	public AjaxResult getDictData(@PathVariable @Validated
								  @NotNull(message = "字典ID不能为空") Long dictId){
		log.info("根据dictId查询字典数据,参数：{}",dictId);
		DictData dictData =dictDataService.selectDictDataById(dictId);
		
		return AjaxResult.success(dictData);
	}
	
	/**
	 * 添加字典数据
	 * @param dictDataDto
	 * @return
	 */
	@PostMapping("addDictData")
	public AjaxResult addDictData(@Validated DictDataDto dictDataDto){
		log.info("添加字典数据,参数：{}",dictDataDto);
		//从Shiro中获取当前用户
		dictDataDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
		int rows = dictDataService.insert(dictDataDto);
		//返回结果数字代表新增成功
		return AjaxResult.toAjax(rows);
	}
	
	/**
	 * 修改字典数据
	 * @param dictDataDto
	 * @return
	 */
	@PostMapping("updateDictData")
	public AjaxResult updateDictData(@Validated DictDataDto dictDataDto){
		log.info("修改字典数据,参数：{}",dictDataDto);
		
		dictDataDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
		int rows = dictDataService.update(dictDataDto);
		return AjaxResult.toAjax(rows);
	}
	
	/**
	 * 删除字典数据
	 * @param dictIds
	 * @return
	 */
	@DeleteMapping("deleteDictData/{dictIds}")
	public AjaxResult deleteDictData(@PathVariable
										 @Validated
										 @NotEmpty(message = "字典ID不能为空") Long[] dictIds){
		log.info("删除字典数据,参数：{}",dictIds);
		
		int rows = dictDataService.deleteDictData(dictIds);
		return AjaxResult.toAjax(rows);
	}
	
	
}
